package com.colorlight.cloud.gateway.service.impl;


import com.colorlight.cloud.auth.util.TopicUtil;
import com.colorlight.cloud.gateway.api.message.constraint.protocol.ProtocolType;
import com.colorlight.cloud.gateway.api.message.constraint.schedule.ScheduleActions;
import com.colorlight.cloud.gateway.api.message.instruct.api.PublishCommand;
import com.colorlight.cloud.gateway.api.message.model.CommandData;
import com.colorlight.cloud.gateway.service.CommandService;
import com.colorlight.cloud.mqtt.base.core.properties.MqttProperties;
import org.springframework.stereotype.Service;

/**
 * @author Demon
 * 部分操作不支持指令（上报类型）
 */
@Service
public class CommandServiceImpl implements CommandService {
	
	
	private final MqttProperties properties;
	private final PublishCommand publishCommand;
	
	public CommandServiceImpl(MqttProperties properties, PublishCommand publishCommand) {
		this.properties = properties;
		this.publishCommand = publishCommand;
	}
	
	
	@Override
	public CommandData<? extends ScheduleActions> builderCommandData(ScheduleActions scheduleActions, String topic, String type, String screenId, Long seqId) {
		CommandData<ScheduleActions> commandData = new CommandData<>();
		commandData.setContent(scheduleActions);
		commandData.setTopic(topic);
		commandData.setType(type);
		commandData.setUuid(screenId);
		commandData.setSeqId(seqId);
		return commandData;
	}
	
	@Override
	public Long publish(ScheduleActions scheduleActions, String deviceName,String  productKey, String screenId, Long seqId) {
		ProtocolType protocolType = scheduleActions.getProtocolType();
		String type = protocolType.getThisName();
		String publisherTopic = TopicUtil.getPublisherTopic(productKey, deviceName);
		CommandData<? extends ScheduleActions> commandData = this.builderCommandData(scheduleActions, publisherTopic, type, screenId, seqId);
		return publishCommand.publishCommandData(commandData);
		
	}
}
